# Archlive <http://archlive-pkg.googlecode.com>

_kernver=`uname -r`

pkgname=(oss oss-build-mods oss-utils)
pkgver=926
pkgrel=1
pkgdesc="Open Sound System UNIX audio architecture"
arch=('i686' 'x86_64')
url="http://www.opensound.com/"
license=('GPL2')
depends=('module-init-tools' 'libtool' 'sed')
makedepends=('pkgconfig' 'gawk' 'gtk2' 'mercurial' 'gcc' 'make')
conflicts=('oss-linux' 'oss-linux-free' 'oss-testing' 'oss-mercurial')
source=('oss.rm-init-scripts.patch' 'oss.soundon.patch' 'oss.rc' 'oss.install' \
	 'ossxmix.png' 'ossxmix.desktop' \
	'build_oss' 'PKGBUILD.buildmods' )
optdepends=()
md5sums=()

_hgroot="http://opensound.hg.sourceforge.net:8000/hgroot/opensound/opensound"
_hgrepo="oss"

# oss kernel modules dir name
TARGET="oss-kernel-modules"

build() {

  cd $startdir

  if [ -d ${_hgrepo} ]; then
      cd ${_hgrepo} && hg pull -u -r ${pkgver}
  else
      hg clone ${_hgroot} ${_hgrepo} || return 1
  fi
  
  [ -d $srcdir/${_hgrepo} ] && rm -rf $srcdir/${_hgrepo}
  hg clone $startdir/${_hgrepo} $srcdir/${_hgrepo}
  cd $srcdir/${_hgrepo}

  # Avoid these flags conflicting with OSS build system.
  unset CFLAGS
  unset OSFLAGS
  unset LIBRARIES
  export NO_WARNING_CHECKS=yes

  # Compile libflashsupport.so only in packaging time, so we avoid
  # conflicts with other packages and ease package management.
  msg "Building libflashsupport.so."
  cd "oss/lib"
  gcc -shared -fPIC -O2 -Wall -Werror flashsupport.c -o libflashsupport.so || return 1
  #rm -f "flashsupport.c" "libflashsupport.so" &> /dev/null

  msg "Preparing the build environment."
  cd $srcdir/${_hgrepo}
  [ -d build ] && rm -rf build
  mkdir build && cd build
  ../configure --regparm --config-midi=YES --enable-timings --portable-build
  # --enable-libsalsa=YES || return 1

  msg "Building OSS."
  make build || return 1

  msg "Patching init scripts."
  cd prototype
  rm usr/lib/oss/etc/S89oss
  patch -Np0 -i "$srcdir/oss.rm-init-scripts.patch" || return 1
  patch -Np0 -i "$srcdir/oss.soundon.patch" || return 1

  find usr/sbin usr/bin -type f -exec chmod +x {} \;
##############################
	msg "Build kernel modules for $(uname -r)"
	target=$srcdir/$TARGET/
	mkdir -p $target
	workdir=$(mktemp -du /tmp/oss.XXXXXX)
	cp -a $srcdir/oss/build/prototype/usr/lib/oss "${workdir}/"
	cd "${workdir}"
	source $startdir/build_oss
	_compile_mods "${workdir}" "$target"
  	rm -rf "${workdir}"
##############################
}

package_oss() {
	pkgdesc="Open Sound System kernel modules"
	depends=("kernel26=${_kernver}" oss-utils)
	provides=('oss' 'oss4')
	install=oss.install

	msg "Install pre-complied kernel modules..."
	cd $srcdir/$TARGET
	target="$pkgdir/lib/modules/$(uname -r)/kernel/oss"
	mkdir -p $target
	cp * $target/
	find "$target" -name '*.ko' -exec gzip -9 {} \;
	msg "Done."

	oss_ver=$(sh $srcdir/oss/showversion.sh)
	oss_ver=${oss_ver/v/}
	pkgver=${oss_ver}.$pkgver
	pkgrel=${_kernver/-/_}
	sed -i -e "s/KERNEL_VERSION=.*/KERNEL_VERSION=${_kernver}/" $startdir/*.install
}

package_oss-build-mods() {
	pkgdesc="Open Sound System kernel sources and auto build system."
	install=oss-build-mods.install

	cd $srcdir/oss/build/prototype
	mkdir -p "${pkgdir}/usr/bin"
	cp -r etc "${pkgdir}/"
	cp -r usr/lib "${pkgdir}/usr/"
	chmod -R 755 "${pkgdir}/usr/lib/oss"
	install -m755 $startdir/build_oss ${pkgdir}/usr/bin/build_oss
	mv ${pkgdir}/usr/lib/oss/build/install.{sh,sh.bak}

	install -m755 $startdir/PKGBUILD.buildmods ${pkgdir}/usr/lib/oss/PKGBUILD
	install -m755 $startdir/oss.install ${pkgdir}/usr/lib/oss/oss.install

	oss_ver=$(sh $srcdir/oss/showversion.sh)
	oss_ver=${oss_ver/v/}
	pkgver=${oss_ver}.$pkgver

	sed -i -e "s/pkgver=.*/pkgver=$pkgver/" ${pkgdir}/usr/lib/oss/PKGBUILD
	sed -i -e "s/oss_ver=.*/oss_ver=$pkgver/" ${pkgdir}/usr/bin/build_oss
}

package_oss-utils() {
	pkgdesc="Open Sound System Utils."
	#install=oss-utils.install
	optdepends=('gtk2: for graphical mixer--ossxmix'
		'hal: for automatic USB audio configuration')
	backup=('usr/lib/oss/soundon.user')

	msg "Copying files."
	cd $srcdir/oss/build/prototype
	cp -r usr "${pkgdir}/"
	rm -rf "${pkgdir}/usr/lib"
	chmod -R a+r "${pkgdir}" 
	find "${pkgdir}" -type d -exec chmod a+x '{}' \;
	install -D -m755 "$srcdir/oss.rc" "$pkgdir/etc/rc.d/oss4"
	install -D -m755 "$srcdir/ossxmix.desktop" "$pkgdir/usr/share/applications/ossxmix.desktop"
	install -D -m644 "$srcdir/ossxmix.png" "$pkgdir/usr/share/pixmaps/ossxmix.png"
	install -Dm755 usr/lib/oss/lib/libflashsupport.so "${pkgdir}/usr/lib/libflashsupport.so" || return 1

	oss_ver=$(sh $srcdir/oss/showversion.sh)
	oss_ver=${oss_ver/v/}
	pkgver=${oss_ver}.$pkgver
}
md5sums=('b9a380a0ac8896390d71ac13676f27e1'
         '65f07fe241bfbf912f76d8b6d8f276b5'
         'b70f139f73c2f1ee9db2680ce48121fb'
         '2e82553664f4212cf28a29be263b5dec'
         'f6a0fa9d274d21ab92cd03fca885add7'
         '81e0f51ec04379dee64c31c32b045028'
         'ae020d09e85e6086efda8335542e5ad9'
         '65f5562c1553dd0f1251674e14a0ff69')
